using System.Collections.Generic;
using System.IO;
using Core;

namespace DoItNow.Features
{
	public class ToggleBetweenHeaderAndCppFeature : BindableCommandFeature
	{
        public ToggleBetweenHeaderAndCppFeature(Ide ide, OutputPane outputPane)
			: base("Toggle Header And Cpp", "Text Editor::alt+o", ide, outputPane)
		{
		}

		public override bool IsAvailable
		{
			get
			{
				if (Ide.ActiveDocument == null)
					return false;

				return (Ide.ActiveDocument.Language & ProgrammingLanguage.CPlusPlus) > 0;
			}
		}

		public override void OnExecute()
		{
			CodeDocument activeDocument = Ide.ActiveDocument;

			string toggledDocumentName = GetToggledDocumentName(activeDocument.FilePath);

			var sourceFiles = Ide.FindFilesInSolution();
			var candidates = sourceFiles.FindAll(match => Path.GetFileName(match) == toggledDocumentName);

            if (candidates.Count > 0)
			    Ide.OpenFile(candidates[0]);
		}

		public string GetToggledDocumentName(string path)
		{
			var toggledPath = Path.ChangeExtension(path, (Path.GetExtension(path) == ".h") ? ".cpp" : ".h");
			return Path.GetFileName(toggledPath);
		}
	}
}